import { Injectable } from '@nestjs/common';
import { CreateTenantDto } from './dto/create-tenant.dto';
import { UpdateTenantDto } from './dto/update-tenant.dto';
import { ResultData } from '@/common/utils/result';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { SysTenantEntity } from './entities/tenant.entity';

@Injectable()
export class TenantService {
  constructor(
    @InjectRepository(SysTenantEntity)
    private readonly sysTenantEntityRep: Repository<SysTenantEntity>,
  ) {}
  async create(createTenantDto: CreateTenantDto) {
    const hasId = await this.sysTenantEntityRep.findOne({
      where: {
        tenantCode: createTenantDto.tenantCode,
      },
    });
    if (hasId) {
      return ResultData.fail(500, `已存在${createTenantDto.tenantCode}`);
    }
    await this.sysTenantEntityRep.save(createTenantDto);
    return ResultData.ok();
  }

  findAll() {
    return `This action returns all tenant`;
  }

  findOne(id: number) {
    return `This action returns a #${id} tenant`;
  }

  update(id: number, updateTenantDto: UpdateTenantDto) {
    return `This action updates a #${id} tenant`;
  }

  remove(id: number) {
    return `This action removes a #${id} tenant`;
  }
}
